{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# HW3: Estimation and approximation in linear regression\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "import numpy as np\n",
                "import matplotlib.pyplot as plt\n",
                "%matplotlib inline\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Part (e)\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "deg = 12 # degree of the polynomial\n",
                "step = 80\n",
                "n_s = 40 # intial sample size\n",
                "error = np.zeros((deg, step)) # defining error as a function of d and n\n",
                "w0, w1 = 1, 1\n",
                "# generate variables: alpha, z, y_s, y\n",
                "### start 1 ###\n",
                "\n",
                "### end 1 ###\n",
                "\n",
                "for N in range(step): # number of steps\n",
                "    n = N + n_s # sample size at each step\n",
                "\n",
                "    for d in range(1, deg + 1): # degree of polynomial from 1 to deg\n",
                "        ### start 2 ###\n",
                "\n",
                "        ### end 2 ###\n",
                "\n",
                "plt.figure(figsize=(10, 4))\n",
                "plt.subplot(121)\n",
                "plt.semilogy(np.arange(1, deg + 1), error[:, -1])\n",
                "plt.xlabel('degree of polynomial (D)')\n",
                "plt.ylabel('log of error (n=120)')\n",
                "plt.subplot(122)\n",
                "plt.semilogy(np.arange(n_s, n_s + step), error[-1, :])\n",
                "plt.xlabel('number of samples (n)')\n",
                "plt.ylabel('log of error (D=12)')\n",
                "plt.tight_layout()\n",
                "plt.show()\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "## Part (g)\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "deg = 12 # degree of the polynomial\n",
                "step = 80\n",
                "n_s = 40 # intial sample size\n",
                "error = np.zeros((deg, step)) # defining error as a function of d and n\n",
                "# generate variables: alpha, z, y_s, y\n",
                "### start 3 ###\n",
                "\n",
                "### end 3 ###\n",
                "\n",
                "for N in range(step): # number of steps\n",
                "    n = N + n_s # sample size at each step\n",
                "\n",
                "    for d in range(1, deg + 1): # degree of polynomial from 1 to deg\n",
                "        ### start 4 ###\n",
                "\n",
                "        ### end 4 ###\n",
                "\n",
                "plt.figure(figsize=(10, 4))\n",
                "plt.subplot(121)\n",
                "plt.semilogy(np.arange(1, deg + 1), error[:, -1])\n",
                "plt.xlabel('degree of polynomial (D)')\n",
                "plt.ylabel('log of error (n=120)')\n",
                "plt.subplot(122)\n",
                "plt.semilogy(np.arange(n_s, n_s + step), error[-1, :])\n",
                "plt.xlabel('number of samples (n)')\n",
                "plt.ylabel('log of error (D=12)')\n",
                "plt.tight_layout()\n",
                "plt.show()\n"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.8.3"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 4
}